package com.store.satring.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StoreUtility {

	public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static boolean isDatesEqual(Date date1, Date date2) {
		boolean datesEqual = false;

		Calendar calendar1 = getCalendarFromDate(date1);
		Calendar calendar2 = getCalendarFromDate(date2);

		if (calendar1.equals(calendar2)) {
			datesEqual = true;
		}

		return datesEqual;
	}

	public static Calendar getCalendarFromDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	public static Date getOnlyDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.AM_PM, Calendar.AM);
		return calendar.getTime();
	}

	public static double getAmount(int scale, double amount) {
		BigDecimal bd = new BigDecimal(amount)
				.setScale(scale, RoundingMode.HALF_UP);
		return bd.doubleValue();
	}
	
	public static String getRouteCauseMessage(Exception e) {
		e.printStackTrace();
		while (e != null && e.getCause() != null) {
			e = (Exception) e.getCause();
		}
		return e.getMessage() + "(see server console for stack trace...)";
	}

}
